package singleton;

/**
 * 无锁单例
 *
 * 目的: 避免同步锁 & 判断带来的开销
 * 实现:
 * 1. 声明时直接实例化
 *
 * @author insight
 * @since 2021/4/22
 */
public class SingletonWithoutSyncInner {

    /**
     * 使用内部类 在返回实例时才初始化 保证懒加载
     */
    public static class SingletonHolder {
        private static final SingletonWithoutSyncInner INSTANCE =
                new SingletonWithoutSyncInner();
    }

    private SingletonWithoutSyncInner() {
        System.out.println("SingletonWithoutSync is Instantiated!");
        System.out.println(SingletonHolder.INSTANCE != null);
    }

    public static SingletonWithoutSyncInner getInnerInstance() {
        System.out.println("inner: ");
        System.out.println(SingletonHolder.INSTANCE != null);
        return SingletonHolder.INSTANCE;
    }
}